﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Smo.Agent;
using Microsoft.SqlServer.Management.Nmo;
//using Microsoft.SqlServer.ConnectionInfo;
using Microsoft.SqlServer.Management;
using System.Data.Sql;
using System.Data.SqlClient;
using Entity;
using DataControls;

namespace GiaoVien_QuanLy_DeTai
{
    public partial class GIAOVIEN_Form : Form
    {
        public GIAOVIEN_Form()
        {
            InitializeComponent();
        }
        GIAOVIEN_Data giaovien_Data = new GIAOVIEN_Data();
        KHOA_Data khoaData = new KHOA_Data();
        GIAOVIEN gv = new GIAOVIEN();
        HOCVI_Data hocviData = new HOCVI_Data();


        private void GIAOVIEN_Form_Load(object sender, EventArgs e)
        {
            cbHocVi.Items.Clear();
            cbKhoa.Items.Clear();
            hocviData.hocvi_Combobox(cbHocVi);
            khoaData.khoa_Combobox(cbKhoa);

            txtMaGV.Text = "GV";
            txtQueQuan.Text = "";
            txtTenGiaoVien.Text = "";
            txtDiaChi.Text = "";
            rdbNam.Checked = true;
            dtNBDLV.Value = DateTime.Now.Date;
            dtNgaySinh.Value = Convert.ToDateTime("1/1/1990");
        }

        private void btnHienThi_Click(object sender, EventArgs e)
        {
            gv.hienthi_GiaoVien(dtgGiaoVien);
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtMaGV.Text == "")
            {
                MessageBox.Show("Ban chua dien Ma Giao Vien");
                txtMaGV.Focus();
                return;
            }

            else if (txtTenGiaoVien.Text == "")
            {
                MessageBox.Show("Ban chua dien Ten Giao Vien");
                txtTenGiaoVien.Focus();
                return;
            }

            else if (txtQueQuan.Text == "")
            {
                MessageBox.Show("Ban chua dien Que Quan Giao Vien");
                txtQueQuan.Focus();
                return;
            }

            else if (txtDiaChi.Text == "")
            {
                MessageBox.Show("Ban chua dien Dia Chi Giao Vien");
                txtDiaChi.Focus();
                return;
            }
            else if (cbKhoa.Text == "")
            {
                MessageBox.Show("Ban chua chon Khoa Giao Vien");
                return;
            }
            else if (cbHocVi.Text == "")
            {
                MessageBox.Show("Ban chua chon Hoc Vi Giao Vien");
                return;
            }
            else if (dtNgaySinh.Value.Year + 18 >= dtNBDLV.Value.Year)
            {
                MessageBox.Show("Ngay Sinh va Ngay Lam Viec khong hop le");
                return;
            }
            else if (giaovien_Data.kiemtra_MaGiaoVien(txtMaGV.Text.ToString()))
            {
                MessageBox.Show("Ban nhap trung Ma Giao Vien");
                txtMaGV.Focus();
                return;
            }

            else
            {
                gv.maGV = txtMaGV.Text.ToString();
                gv.tenGV = txtTenGiaoVien.Text.ToString();
                gv.diachi = txtDiaChi.Text.ToString();
                gv.quequan = txtQueQuan.Text.ToString();

                if (rdbNam.Checked == true)
                    gv.gioitinh = "nam";
                else if (rdbNu.Checked == true)
                    gv.gioitinh = "nu";
                if (giaovien_Data.kiemtra_GiaoVien(gv))
                {
                    MessageBox.Show("Giao vien nay da ton tai");
                    txtTenGiaoVien.Focus();
                    return;
                }
                gv.ngaysinh = dtNgaySinh.Value;
                gv.ngBDL = dtNBDLV.Value;

                gv.hesoluong = (float)(Convert.ToDouble(txtHeSoLuong.Text));
                gv.mucluong = (float)(Convert.ToDouble(txtMucLuong.Text));
                gv.maKhoa = ((ListItem)(cbKhoa.SelectedItem)).value;
                gv.maHocvi = ((ListItem)(cbHocVi.SelectedItem)).value;

                gv.them_GiaoVien(gv);

                gv.hienthi_GiaoVien(dtgGiaoVien);

                txtMucLuong.Text = "";
                txtHeSoLuong.Text = "";
                txtMaGV.Text = "GV";
                txtQueQuan.Text = "";
                txtTenGiaoVien.Text = "";
                txtDiaChi.Text = "";
                rdbNam.Checked = true;
                dtNBDLV.Value = DateTime.Now.Date;
                dtNgaySinh.Value = Convert.ToDateTime("1/1/1990");


            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaGV.Text == "")
            
            {
                MessageBox.Show("Ban chua nhap Ma Giao Vien");
                txtMaGV.Focus();
                txtMucLuong.Text = "";
                txtHeSoLuong.Text = "";
                txtMaGV.Text = "";
                txtQueQuan.Text = "";
                txtTenGiaoVien.Text = "";
                txtDiaChi.Text = "";
                rdbNam.Checked = true;
                dtNBDLV.Value = DateTime.Now.Date;
                dtNgaySinh.Value = Convert.ToDateTime("1/1/1990");
                return;
            }
            else if (giaovien_Data.kiemtraMaGV_CV(txtMaGV.Text.ToString()))
            {
                MessageBox.Show("Ban chua xoa giao vien trong bang Chuc Vu");
                return;
            }
            else if (giaovien_Data.kiemtraMaGV_DETAI(txtMaGV.Text.ToString()))
            {
                MessageBox.Show("Ban chua xoa giao vien trong bang De Tai");
                return;
            }
            else if (giaovien_Data.kiemtraMaGV_GIANGDAY(txtMaGV.Text.ToString()))
            {
                MessageBox.Show("Ban chua xoa giao vien trong bang Giang Day");
                return;
            }
            else if (giaovien_Data.kiemtraMaGV_LOPHOC(txtMaGV.Text.ToString()))
            {
                MessageBox.Show("Ban chua xoa giao vien trong bang Lop Hoc");
                return;
            }
            else if (giaovien_Data.kiemtraMaGV_LUONGCB(txtMaGV.Text.ToString()))
            {
                MessageBox.Show("Ban chua xoa giao vien trong bang Luong Can Ban");
                return;
            }
            else if (giaovien_Data.kiemtraMaGV_LUONGGD(txtMaGV.Text.ToString()))
            {
                MessageBox.Show("Ban chua xoa giao vien trong bang Luong Giang Day");
                return;
            }
            else if (giaovien_Data.kiemtraMaGV_LUONGKN(txtMaGV.Text.ToString()))
            {
                MessageBox.Show("Ban chua xoa giao vien trong bang Luong Kiem Nghiem");
                return;
            }
            else if (giaovien_Data.kiemtraMaGV_NXDT(txtMaGV.Text.ToString()))
            {
                MessageBox.Show("Ban chua xoa giao vien trong bang Nhan Xet De Tai");
                return;
            }
                gv.xoa_GiaoVien(txtMaGV.Text.ToString());
                txtMucLuong.Text = "";
                txtHeSoLuong.Text = "";
                txtMaGV.Text = "GV";
                txtQueQuan.Text = "";
                txtTenGiaoVien.Text = "";
                txtDiaChi.Text = "";
                rdbNam.Checked = true;
                dtNBDLV.Value = DateTime.Now.Date;
                dtNgaySinh.Value = Convert.ToDateTime("1/1/1990");

                gv.hienthi_GiaoVien(dtgGiaoVien);
            
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (txtMaGV.Text == "")
            {
                MessageBox.Show("Ban chua dien Ma Giao Vien");
                txtMaGV.Focus();
                return;
            }

            else if (txtTenGiaoVien.Text == "")
            {
                MessageBox.Show("Ban chua dien Ten Giao Vien");
                txtTenGiaoVien.Focus();
                return;
            }

            else if (txtQueQuan.Text == "")
            {
                MessageBox.Show("Ban chua dien Que Quan Giao Vien");
                txtQueQuan.Focus();
                return;
            }

            else if (txtDiaChi.Text == "")
            {
                MessageBox.Show("Ban chua dien Dia Chi Giao Vien");
                txtDiaChi.Focus();
                return;
            }
            else if (cbKhoa.Text == "")
            {
                MessageBox.Show("Ban chua chon Khoa Giao Vien");
                return;
            }
            else if (cbHocVi.Text == "")
            {
                MessageBox.Show("Ban chua chon Hoc Vi Giao Vien");
                return;
            }
            else if (dtNgaySinh.Value.Year + 18 >= dtNBDLV.Value.Year)
            {
                MessageBox.Show("Ngay Sinh va Ngay Lam Viec khong hop le");
                return;
            }

            else
            {
                gv.maGV = txtMaGV.Text.ToString();
                gv.tenGV = txtTenGiaoVien.Text.ToString();
                gv.diachi = txtDiaChi.Text.ToString();
                gv.quequan = txtQueQuan.Text.ToString();

                if (rdbNam.Checked == true)
                    gv.gioitinh = "nam";
                else if (rdbNu.Checked == true)
                    gv.gioitinh = "nu";

                gv.ngaysinh = dtNgaySinh.Value;
                gv.ngBDL = dtNBDLV.Value;

                gv.hesoluong = (float)(Convert.ToDouble(txtHeSoLuong.Text));
                gv.mucluong = (float)(Convert.ToDouble(txtMucLuong.Text));
                gv.maKhoa = ((ListItem)(cbKhoa.SelectedItem)).value;
                gv.maHocvi = ((ListItem)(cbHocVi.SelectedItem)).value;


                gv.sua_GiaoVien(gv);
                gv.hienthi_GiaoVien(dtgGiaoVien);

                txtMucLuong.Text = "";
                txtHeSoLuong.Text = "";
                txtMaGV.Text = "GV";
                txtQueQuan.Text = "";
                txtTenGiaoVien.Text = "";
                txtDiaChi.Text = "";
                rdbNam.Checked = true;
                dtNBDLV.Value = DateTime.Now.Date;
                dtNgaySinh.Value = Convert.ToDateTime("1/1/1990");
            }
        }

        private void dtgGiaoVien_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dtgGiaoVien.AllowUserToAddRows = false;
            int dong = e.RowIndex;
            txtMaGV.Text = dtgGiaoVien.Rows[dong].Cells[0].Value.ToString();
            txtTenGiaoVien.Text = dtgGiaoVien.Rows[dong].Cells[1].Value.ToString();
            txtDiaChi.Text = dtgGiaoVien.Rows[dong].Cells[4].Value.ToString();
            txtQueQuan.Text = dtgGiaoVien.Rows[dong].Cells[5].Value.ToString();
            txtHeSoLuong.Text = dtgGiaoVien.Rows[dong].Cells[8].Value.ToString();
            txtMucLuong.Text = dtgGiaoVien.Rows[dong].Cells[9].Value.ToString();

            String mahocvi=dtgGiaoVien.Rows[dong].Cells[7].Value.ToString();
            giaovien_Data.layten_HocVi(mahocvi,cbHocVi);
            String makhoa=dtgGiaoVien.Rows[dong].Cells[10].Value.ToString();
            giaovien_Data.layten_Khoa(makhoa, cbKhoa);
        }

        private void cbHocVi_SelectedIndexChanged(object sender, EventArgs e)
        {
            String mahocvi = ((ListItem)(cbHocVi.SelectedItem)).value;
            float a = giaovien_Data.hesoluong_GiaoVien(mahocvi);
            txtHeSoLuong.Text = a.ToString();
        }

        private void txtMaGV_MouseClick(object sender, MouseEventArgs e)
        {
            cbHocVi.Items.Clear();
            cbKhoa.Items.Clear();
            hocviData.hocvi_Combobox(cbHocVi);
            khoaData.khoa_Combobox(cbKhoa);

            txtMaGV.Text = "GV";
            txtQueQuan.Text = "";
            txtTenGiaoVien.Text = "";
            txtDiaChi.Text = "";
            txtMucLuong.Text = "";
            txtHeSoLuong.Text = "";
            rdbNam.Checked = true;
            dtNBDLV.Value = DateTime.Now.Date;
            dtNgaySinh.Value = Convert.ToDateTime("1/1/1990");
        }

        private void rdTenKhoa_CheckedChanged(object sender, EventArgs e)
        {
            if (rdTenGiaoVien.Checked)
            {
                txtTGV.Visible = true;
                txtTK.Visible = false;
                txtTGV.Text = "";
            }
            else if (rdTenKhoa.Checked)
            {
                txtTGV.Visible = false;
                txtTK.Visible = true;
                txtTK.Text = "";
            }
        }

        private void rdTenGiaoVien_CheckedChanged(object sender, EventArgs e)
        {
            if (rdTenGiaoVien.Checked)
            {
                txtTGV.Visible = true;
                txtTK.Visible = false;
                txtTGV.Text = "";
            }
            else if (rdTenKhoa.Checked)
            {
                txtTGV.Visible = false;
                txtTK.Visible = true;
                txtTK.Text = "";
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            if (rdTenKhoa.Checked)
            {
                giaovien_Data.timkiemtheo_Khoa(dtgGiaoVien, txtTK.Text.ToString());
                txtTK.Text = "";
            }
            else
            {
                giaovien_Data.timkiemtheo_Ten(dtgGiaoVien, txtTGV.Text.ToString());
                txtTGV.Text = "";
            }
        }


      
       

       
    }
}
